package org.scohen.juploadr.uploadapi.flickrrest.upload;

import org.scohen.juploadr.uploadapi.flickrrest.DefaultFlickrHandler;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

public class GetUploadStatusHandler extends DefaultFlickrHandler {
    private GetUploadStatus parent;

    public GetUploadStatusHandler(Object parent) {
        super(parent);
        this.parent = (GetUploadStatus) parent;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.scohen.juploadr.uploadapi.rest.DefaultFlickrHandler#startElement(java.lang.String,
     *      java.lang.String, java.lang.String, org.xml.sax.Attributes)
     */
    public void startElement(String uri, String localName, String qname,
            Attributes atts) throws SAXException {
        // TODO Auto-generated method stub
        super.startElement(uri, localName, qname, atts);
        lastTag = qname;
        if ("user".equals(qname)) {
            parent.setPro("1".equals(atts.getValue("ispro")));           
        } else if ("bandwidth".equals(qname)) {
            parent.setUnlimited("1".equals(atts.getValue("unlimited")));
            parent.setMaxBandwidth(Long.parseLong(atts.getValue("maxbytes")));
            parent.setUsedBandwidth(parent.getMaxBandwidth() - Long.parseLong(atts.getValue("remainingbytes")));
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.xml.sax.helpers.DefaultHandler#characters(char[], int, int)
     */
    public void characters(char[] chars, int start, int end)
            throws SAXException {
        String cdata = new String(chars, start, end).trim();
        if (cdata.trim().length() > 0) {
            if ("username".equals(lastTag)) {
                parent.setUsername(cdata);
            }
        }
    }

}
